﻿
$(function () {
    //初始化列表信息
    init();
});

//初始化列表信息
function init(queryData) {
    $('#tt').tree({
        url: '/Dictionary/GetDictionaryListByCode?kind=FixedAssetsKind',
        loadFilter: function (rows) {
            return convert(rows);
        },
        onClick: function (node) {
            doSearch();
        }
    });

    $('#list').datagrid({
        title: '固定资产管理',
        idField: 'FixedAssetsID',
        fitColumns: 'true',
        autoRowHeight: true,
        nowrap: true,
        striped: true,
        pagination: true,
        url: '/FixedAssets/GetList',
        columns: [[
			{ field: 'ck', checkbox: true },
			{ field: 'FixedAssetsID', title: '固定资产ID', hidden: true},
			{ field: 'FixedAssetsCode', title: '固定资产编号'},
			{ field: 'FixedAssetsName', title: '固定资产名称'},
			{ field: 'StatusName', title: '状态', width: 300 },
			{ field: 'ManageUserIDName', title: '负责人', width: 200 },
			{ field: 'DepartmentID', title: '所属部门', width: 200 },
			{ field: 'Manufacturers', title: '生产厂家'},
            {
			    field: 'ProductionDateTime', title: '生产日期', width: 300,
			    formatter: function (value, row, index) {
			        return formatDate(value, "yyyy-MM-dd");
			    }
			},
            {
                field: 'PurchaseDateTime', title: '购买日期', width: 300,
                formatter: function (value, row, index) {
                    return formatDate(value, "yyyy-MM-dd");
                }
            },
            {
                field: 'EnableDateTime', title: '启用时间', width: 300,
                formatter: function (value, row, index) {
                    return formatDate(value, "yyyy-MM-dd");
                }
            },
			{ field: 'OriginalValue', title: '原值', width: 180 },
			{ field: 'ScrapValue', title: '残值', width: 180 },
			{ field: 'DepreciationLlife', title: '折旧年限', width: 180 },
			{ field: 'AccumulatedDepreciation.', title: '累计折旧', width: 180 },
			{ field: 'Location', title: '存放地点'},
			{ field: 'FixdAssetsKindName', title: '类别', width: 180, hidden: true },
			{ field: 'HaveAttachment', title: '是否有附件', width: 180, hidden: true },
			{ field: 'Remark', title: '备注', width: 180, hidden: true },
			{ field: 'CreateBy', title: '创建人', width: 180, hidden: true },
			{ field: 'CreateTime', title: '创建时间', width: 180, hidden: true },
			{ field: 'UpdateBy', title: '修改人', width: 180, hidden: true },
			{ field: 'UpdateTime', title: '修改时间', width: 180,hidden:true},
		
        ]],
    });
}

//弹出增加窗口
function add() {
    $('#dlg').dialog('open').dialog('setTitle', '增加');
    $('#fm').form('clear');
    url = '/FixedAssets/Create';
}

//弹出修改窗口
function edit() {
    var row = $('#list').datagrid('getSelected');
    if (row) {
        row["ProductionDateTime"] = formatDate(row["ProductionDateTime"], "yyyy-MM-dd");
        row["PurchaseDateTime"] = formatDate(row["PurchaseDateTime"], "yyyy-MM-dd");
        row["EnableDateTime"] = formatDate(row["EnableDateTime"], "yyyy-MM-dd");
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        $('#fm').form('load', row);
        url = '/FixedAssets/Edit';
    }
}

//保存数据
function save() {
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            if (result == 'OK') {
                $('#dlg').dialog('close');      // close the dialog  
                $('#list').datagrid('reload');    // reload the user data  
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result
                });
            }
        }
    });
}

//删除
function del() {
    var rows = $('#list').datagrid('getSelected');
    if (rows) {
        $.messager.confirm(delDlgTitle, delDlgMsg, function (r) {
            if (r) {
                $.post('/FixedAssets/Delete', { id: rows.FixedAssetsID },
                    function (result) {
                        if (result == 'OK') {
                            $("#list").datagrid('clearSelections');
                            $('#list').datagrid('reload');    // reload the user data  
                        } else {
                            $.messager.show({   // show error message  
                                title: 'Error',
                                msg: result
                            });
                        }
                    });
            }
        });
    }
}
//搜索
function doSearch() {
    var node = $('#tt').tree('getSelected');
    $('#list').datagrid('load', {
        FixedAssetsName: $('#FixedAssetsName').val(),
        DictionaryKind: node.id
    });
}
